﻿using System;
using fastCSharp;
using System.Threading;
using System.Reflection;

namespace diantou.webSocket
{
    class Program
    {
        /// <summary>
        /// Http服务客户端
        /// </summary>
        private static fastCSharp.net.tcp.http.servers.tcpClient httpClient;
        /// <summary>
        /// 考试用户图片域名
        /// </summary>
        private static fastCSharp.net.tcp.http.domain examImageServerDomain = fastCSharp.config.pub.LoadConfig(new fastCSharp.net.tcp.http.domain(), "examImage");
        /// <summary>
        /// 考试日志域名
        /// </summary>
        private static fastCSharp.net.tcp.http.domain examLogServerDomain = fastCSharp.config.pub.LoadConfig(new fastCSharp.net.tcp.http.domain(), "examLog");

        static void Main(string[] args)
        {
            if (fastCSharp.config.appSetting.IsCheckMemory) checkMemory.Add(typeof(Program));
            while (true)
            {
                try
                {
                    using (httpClient = new fastCSharp.net.tcp.http.servers.tcpClient())
                    {
                        using (fastCSharp.threading.task task = new fastCSharp.threading.task(4))
                        {
                            httpClient.stop(new fastCSharp.net.tcp.http.domain[] { examImageServerDomain, examLogServerDomain });
                            task.Add(webSocket);
                        }
                        Console.WriteLine("Finally");
                        return;
                    }
                }
                catch (Exception error)
                {
                    output(error);
                }
                Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="error"></param>
        private static void output(Exception error)
        {
            Console.WriteLine(error.ToString());
            log.Default.Real(error, null, false);
        }
        /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="message"></param>
        private static void output(string message)
        {
            Console.WriteLine(message);
            log.Default.Real(message, new System.Diagnostics.StackFrame(), true);
        }
        /// <summary>
        /// 启动WebSocket域名
        /// </summary>
        private static void webSocket()
        {
            while (true)
            {
                try
                {
                    fastCSharp.net.tcp.http.servers.startState state = httpClient.start(Assembly.GetExecutingAssembly().Location, typeof(webServer).FullName, new fastCSharp.net.tcp.http.domain[] { examImageServerDomain, examLogServerDomain }, true);
                    output("webSocket " + state.ToString());
                    if (state == fastCSharp.net.tcp.http.servers.startState.Success) return;
                }
                catch (Exception error)
                {
                    output(error);
                }
                Thread.Sleep(1000);
            }
        }
    }
}
